<template>
  <div class="wrap-container sn-container">
    <div class="sn-content">
      <div class="sn-title">
        双轴柱状图
        <div class="sn-title-more">
          <a href="#">
            <img src="@/assets/img/sn-title.png"/>
          </a>
        </div>
      </div>
      <div class="sn-body">
        <div class="wrap-container">
          <div id="bottomLeftChart" class="bottomLeftChart"></div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
  import {nextTick} from 'vue'
  export default {
    name: 'szbarEchart',
    data() {
      return {
        szbarOption: null
      };
    },
    mounted() {
      this.getOption();
      let chartSzbar = {
        id: 'bottomLeftChart',
        chart: null,
        options: this.szbarOption
      }
      nextTick(()=>{
        chartSzbar.chart = this.$echarts.init(document.getElementById(chartSzbar.id));
        chartSzbar.chart.setOption(chartSzbar.options);
      })
      window.addEventListener('resize',()=>{
        chartSzbar.chart.resize()
      })
    },
    methods: {
      getOption() {
        let category = [
          "市区",
          "万州",
          "江北",
          "南岸",
          "北碚",
          "綦南",
          "长寿",
          "永川",
          "璧山",
          "江津",
          "城口",
          "大足"
        ];
        let lineData = [
          18092,
          20728,
          24045,
          28348,
          32808,
          36097,
          39867,
          44715,
          48444,
          50415,
          56061,
          62677
        ];
        let barData = [
          8500,
          9900,
          12500,
          14000,
          21500,
          23200,
          24450,
          25250,
          33300,
          4600,
          5000,
          23452
        ];
        let rateData = [];
        for (let i = 0; i < 33; i++) {
          let rate = barData[i] / lineData[i];
          rateData[i] = rate.toFixed(2);
        }

        let option = {
          title: {
            text: "",
            x: "center",
            y: 0,
            textStyle: {
              color: "#B4B4B4",
              fontSize: 16,
              fontWeight: "normal"
            }
          },
          tooltip: {
            trigger: "axis",
            backgroundColor: "rgba(255,255,255,0.1)",
            axisPointer: {
              type: "shadow",
              label: {
                show: true,
                backgroundColor: "#7B7DDC"
              }
            }
          },
          legend: {
            data: ["已贯通", "计划贯通", "贯通率"],
            textStyle: {
              color: "#B4B4B4"
            },
            top: "0%"
          },
          grid: {
            x: "8%",
            width: "88%",
            y: "4%"
          },
          xAxis: {
            data: category,
            axisLine: {
              lineStyle: {
                color: "#B4B4B4"
              }
            },
            axisTick: {
              show: false
            }
          },
          yAxis: [
            {
              splitLine: { show: false },
              axisLine: {
                lineStyle: {
                  color: "#B4B4B4"
                }
              },

              axisLabel: {
                formatter: "{value} "
              }
            },
            {
              splitLine: { show: false },
              axisLine: {
                lineStyle: {
                  color: "#B4B4B4"
                }
              },
              axisLabel: {
                formatter: "{value} "
              }
            }
          ],
          series: [
            {
              name: "贯通率",
              type: "line",
              smooth: true,
              showAllSymbol: true,
              symbol: "emptyCircle",
              symbolSize: 8,
              yAxisIndex: 1,
              itemStyle: {
                normal: {
                  color: "#F02FC2"
                }
              },
              data: rateData
            },

            {
              name: "已贯通",
              type: "bar",
              barWidth: 10,
              itemStyle: {
                normal: {
                  barBorderRadius: 5,
                  color: new this.$echarts.graphic.LinearGradient(0, 0, 0, 1, [
                    { offset: 0, color: "#956FD4" },
                    { offset: 1, color: "#3EACE5" }
                  ])
                }
              },
              data: barData
            },

            {
              name: "计划贯通",
              type: "bar",
              barGap: "-100%",
              barWidth: 10,
              itemStyle: {
                normal: {
                  barBorderRadius: 5,
                  color: new this.$echarts.graphic.LinearGradient(0, 0, 0, 1, [
                    { offset: 0, color: "rgba(156,107,211,0.8)" },
                    { offset: 0.2, color: "rgba(156,107,211,0.5)" },
                    { offset: 1, color: "rgba(156,107,211,0.2)" }
                  ])
                }
              },
              z: -12,

              data: lineData
            }
          ]
        };
        this.szbarOption = option;
      }
    }
  };
</script>

<style lang="scss" scoped>
  .sn-container {
    left: 25.5%;
    width: 48.5%;
    height: 100%;

    .bottomLeftChart {
      width: 100%;
      height: 95%;
      top: 1rem;
    }
  }
</style>